<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class UserController extends Controller{
    public function admin(){
        return view('admin.admin-index');
    }
    public function index(){
        $oUsers = User::paginate(3);
        return view('admin.user-list')->with('oUsers',$oUsers);
    }
    public function login(){
        return view('admin.login');
    }
    public function dologin(Request $request){
        $name=$request->name;
        $password=$request->password;
        if(Auth::attempt(['name'=>$name,'password'=>$password])){
            return json_encode(['success'=>true,'msg'=>"用户成功登录"]);
        }else{
            return json_encode(['success'=>false,'msg'=>'用户名或密码错误']);
        }
    }
    public function logout(){
        Auth::logout();
        return redirect('/');
    }
    public function userlist(){
        $oUsers=User::orderby('id','desc')->paginate(6);//分页
        return view('admin.user-list')->with('oUsers',$oUsers);
    }
    public function create(){
        return view('admin.user-create');
    }
    public function store(Request $request){
        $datas=$request->all();
        $rules=[
            'name'=>'required|alpha_dash|min:3|unique:users',
            'password'=>'required|min:6|confirmed'
        ];
        $message=[
            'name.required'=>'用户名不能为空',
            'name.alpha_dash'=>'用户名只能包含数字，字母，横线-或下划线_，不能包含其他字符',
            'name.min'=>'用户名太短，至少要有:min个字符',
            'name.unique'=>'用户名已存在，不能重复注册',
            'password.required'=>'密码不能为空',
            'password.min'=>'密码长度不能少于:min个字符',
            'password.confirmed'=>'两次输入密码不一致',
        ];
        $validate=Validator::make($datas,$rules,$message);
        if($validate->fails()){
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $datas['password']=Hash::make($datas['password']);
        User::create($datas);
        return json_encode(['success'=>true,'msg'=>'success']);
    }
    public function edit($id){
        $user = User::find($id);
        if($user==NULL){
            abort(404);
        }
        return view('admin.user-edit')->with('user',$user);
    }
    public function update($id,Request $request){
        $user=User::find($id);
        if($user==NULL){
            abort(404);
        }
        $datas=$request->all();
        $rules=[
            'password'=>'required|min:6|confirmed'
        ];
        $message=[
            'password.required'=>'密码不能为空',
            'password.min'=>'密码至少要有:min个字符',
            'password.confirmed'=>'两次输入密码不一致',
        ];
        $validate=Validator::make($datas,$rules,$message);
        if($validate->fails()){
            $msg=$validate->messages()->first();
            return json_encode(['success'=>false,'msg'=>$msg]);
        }
        $user->password=Hash::make($datas['password']);
        $user->save();
        return json_encode(['success'=>true,'msg'=>'success']);
    }
    public function delete($id){
        $user=User::find($id);
        $user->delete();
        return json_encode(['success'=>true,'msg'=>'success']);
    }
}
